package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.ProductContent;
import cn.wolfcode.trip.base.mapper.ProductContentMapper;
import cn.wolfcode.trip.base.mapper.ProductMapper;
import cn.wolfcode.trip.base.query.ProductQuery;
import cn.wolfcode.trip.base.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品业务接口实现类
 */
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductContentMapper contentMapper;

    /**
     * 分页方法
     * @param qo
     * @return
     */
    public PageInfo query(ProductQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Product> list = productMapper.selectForList(qo);
        return new PageInfo(list);
    }

    /**
     * 新增或修改方法
     * @param
     */
    public void saveOrUpdate(Product product) {
        if(product.getId()!=null){
            //更新商品基本信息
            productMapper.updateByPrimaryKey(product);
            //获取内容
            ProductContent productContent = product.getProductContent();
            //主键关联
            productContent.setId(product.getId());
            //更新商品详情内容
            contentMapper.insert(productContent);
        }else{
            //保存商品基本信息
            productMapper.insert(product);
            //获取内容
            ProductContent productContent = product.getProductContent();
            //主键关联
            productContent.setId(product.getId());
            //保存商品详情内容
            contentMapper.insert(productContent);
        }
    }

    /**
     * 根据商品id查询商品详情内容
     */
    @Override
    public ProductContent getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }
}
